package Util::UserTuan::UserTuan;
use Moose;
use namespace::autoclean;
use JSON;

has 'schema' => (
	is => 'rw',
	required => 1,
	isa => 'DBIx::Class::Schema',
);

#####################################################
#Return: 
#	Top ten groups with most members 
#####################################################
sub get_top_five {
	my ($self) = @_;
	my @tuanzhang = $self->schema->resultset('User')->search(
		{'user_tuan.role' => 'C',
		 #'user_tuan_2.role' => 'J'},
		 'user_tuan_2.role' => { -in => ['J','F','Q'] }},
                {
                        '+select' => [
                                {count => 'user_tuan_2.user_id', -as=> 'user_count'}
                        ],
                        group_by => [qw/me.id/],
                        join => {'user_tuan' => 'user_tuan'},
			rows => 10,
			page => 1,
                        order_by => {-desc => [qw/user_count/]}
                }
        );
	return \@tuanzhang;
}

sub search {
	my ($self, $tuan_id, $status) = @_;
	return $self->schema->resultset('UserTuan')->search({tuan_id => $tuan_id, status=> $status});
}

sub find {
	my ($self, $user_tuan_id) = @_;
	return $self->schema->resultset('UserTuan')->search({id => $user_tuan_id});
}

#####################################################
sub find_user_tuan {
	my ($self, $tuan_id, $user_id, $status) = @_;
	return $self->schema->resultset('UserTuan')->search({tuan_id => $tuan_id, user_id => $user_id, role => $status})->first();
}

sub find_user_tuan_by_tuan_id {
	my ($self, $tuan_id) = @_;
	return $self->schema->resultset('UserTuan')->search({tuan_id => $tuan_id});
}

sub find_undelivered_user_tuan_by_tuan_id {
	my ($self, $tuan_id) = @_;
	return $self->schema->resultset('UserTuan')->search({tuan_id => $tuan_id,role => 'J'});
}

sub find_user_tuan_count {
	my ($self, $tuan_id, $user_id, $status) = @_;
	return $self->schema->resultset('UserTuan')->search({tuan_id => $tuan_id})->count();
}

sub find_user_tuan_by_password {
	my ($self, $tihuo_code, $tuan_id) = @_;
	return $self->schema->resultset('UserTuan')->search({tuan_id => $tuan_id, password => $tihuo_code})->first();
}


sub update_contact_info {
	my ($self, $tuan_id, $user_id, $name, $address, $phone, $university, $jt_comment) = @_;
	my $user_tuan = $self->schema->resultset('UserTuan')->search({tuan_id => $tuan_id, user_id => $user_id})->first();
	$user_tuan->update({
		user_name => $name,
		user_address => $address,
		user_cellphone => $phone,
		university_id => $university,
		jt_comment => $jt_comment
	});
	return $user_tuan;
}

sub update_status {
        my ($self, $user_tuan_id, $status) = @_;
        my $user_tuan = $self->schema->resultset('UserTuan')->search({id => $user_tuan_id})->first();
        $user_tuan->update({
                role => $status
        });
        return $user_tuan;
}

sub get_owner {
	my ($self, $tuan_id) = @_;
	return $self->schema->resultset('UserTuan')->search({tuan_id => $tuan_id, role => 'C'});
}

sub get_users {
	my ($self, $tuan_id) = @_;
	return $self->schema->resultset('UserTuan')->search({tuan_id => $tuan_id, role => 'T'});
}

sub get_options {
	my ($self, $user_id, $tuan_id) = @_;
#1 -- delete
#2 -- change to vice tuan zhang
#3 -- change status from new join to deposit
#4 -- change status from deal done to deposit
#5 -- change status from deposit to deal done
#6 -- change to normal user
#7 -- change status from deposit to new join
#8 -- change status from deal done to new join

#new
#1 -- delete
#2 -- change status from new join to deal done 
#3 -- change status from deal done to new join 

        my @options;

        my @arr= $self->schema->resultset('UserTuan')->search({tuan_id => $tuan_id, user_id => $user_id}, {columns => [qw/role status/]});

        if (defined $arr[0]) {
                my $login_user_role = $arr[0]->get_column("role");
                #if($login_user_role eq 'V'){
                #        push(@options, 6);
                #} elsif ($login_user_role eq 'J') {
                #        push(@options, 2);
                #}

                my $login_user_status = $arr[0]->get_column("status");
                if($login_user_status eq 'join'){
                        push(@options, 2);
                #} elsif ($login_user_status eq 'deposit') {
                #        push(@options, 5);
                #        push(@options, 7);
                } else {
                        push(@options, 3);
                }

                push(@options, 1) unless($login_user_role eq 'C');
        }

        return  to_json(\@options);
}

sub get_login_user_role_from_table {
	my ($self, $user, $login_user) = @_;
	my @arr;
        my $login_user_role = '';
        if (defined $user) {
                @arr= $self->schema->resultset('User')->search(
			{'me.id' => $user->id,
			'role.role' => 'Z'}, 
			{
			join => 'role'}
		);
	}
	if(scalar(@arr) > 0 ) {
		$login_user_role = 'Z';
	}
	return $login_user_role;
	
}

sub is_admin {
	my ($self, $user, $login_user) = @_;
	my @arr;
        my $result = '0';
        if (defined $user) {
                @arr= $self->schema->resultset('User')->search(
			{'me.id' => $user->id,
			'role.role' => 'A'}, 
			{
			join => 'role'}
		);
	}
	if(scalar(@arr) > 0 ) {
		$result = '1';
	}
	return $result;
}

sub is_danwei_admin {
	my ($self, $user_id, $login_user_id) = @_;
	my @arr;
        my $result = '0';
	if ($user_id ne $login_user_id ) {
		return '0';
	}
        if (defined $user_id) {
                @arr= $self->schema->resultset('User')->search(
			{'me.id' => $user_id,
			'role.role' => 'B'}, 
			{
			join => 'role'}
		);
	}
	if(scalar(@arr) > 0 ) {
		$result = '1';
	}
	return $result;
}

sub get_login_user_role {
	my ($self, $tuan_id, $user, $login_user) = @_;

	my @arr;
        my $login_user_role = 'X';
        if (defined $user) {
                @arr= $self->schema->resultset('User')->search(
			{'me.id' => $user->id,
			'role.role' => 'A'}, 
			{
			join => 'role'}
		);
		
		if(scalar(@arr) > 0 ) {
			$login_user_role = 'A';
		} else {
			@arr= $self->schema->resultset('UserTuan')->search({tuan_id => $tuan_id, user_id => $user->id}, {columns => [qw/role/]});
			my $string = '';
			foreach(@arr) {
				$string = $string . $_->get_column("role");	
			}
			if ( $string =~ /C/) {
				$login_user_role = 'C';
			} elsif ( $string =~ /J/) {
				$login_user_role = 'J';
			} elsif ( $string =~ /F/) {
				$login_user_role = 'F';
			} elsif ( $string =~ /Q/) {
				$login_user_role = 'Q';
			}
		}
        }

        unless (defined $login_user) {
        #This user did not login yet
                $login_user_role = 'U';
        }

	return $login_user_role;
}

__PACKAGE__->meta->make_immutable;

1;
